<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>computed-watch-methods</title>
  <script src="../vue.js"></script>
</head>
<body>
  <div id="root">
    <!-- {{doubleCount}}
    {{mulCount2(3)}}

    {{enhanceString()}} -->
    <!-- {{swapString}} -->
    <!-- {{count}} -->
    <!-- {{asyncCount}}
    {{total}} -->
    {{stringNumber}}
  </div>
  <script>
    Vue.config.productionTip = false
    var vm = new Vue({
      el: '#root',
      data: {
        count: 1000,
        str: 'hello',
        testStr: 'hello',
        total: 0,
        obj: {
          x: {
            y: 100
          }
        }
      },
      // 计算属性
      // 基于依赖缓存
      computed: {
        doubleCount() {
          console.log(0)
          return this.count * 2
        },
        swapString() {
          return this.str.split('').reverse().join('')
        },

        asyncCount() {
          // setTimeout(() => {
          //   let x = 100
          //   return this.count + x
          // }, 1000)
          // return undefined
        },

        stringNumber: {
          get() {
            return this.count + '!!!'
          },
          set(value) {
            console.log(value)
          }
        }
      },

      methods: {
        mulCount2(size) {
          console.log(100)
          return this.count * size
        },
        enhanceString() {
          return this.testStr + '!!!'
        }
      },

      watch: {
        count(newValue) {
          // setTimeout(() => {
          //   let x = 100
          //   this.total = newValue + x
          // }, 1000)
          this.count += 1
        },
        str: {
          immediate: true,
          handler(newValue) {
            console.log(newValue)
          }
        },
        obj: {
          immediate: true,
          deep: true,
          handler(newValue) {
            console.log(newValue)
          }
        }
      },

      updated() {
        // console.log(this.count)
        // this.count += 1
      },
    })
  </script>
</body>
</html>